package com.winner.data.util;

import org.springframework.util.StopWatch;

import lombok.SneakyThrows;

/**
 * spring包下的StopWatch打印执行时间, 当然cn.hutool.core.date.StopWatch也可以用,用法一样
 *
 * @author wangxinle5
 * @since 2023-04-25
 */
public class PrintTime {

    @SneakyThrows
    public static void main(String[] args) {
        StopWatch sw = new StopWatch();
        sw.start("任务1查询耗时");
        Thread.sleep(1000);
        sw.stop();

        sw.start("任务2查询耗时");
        Thread.sleep(2000);
        sw.stop();

        // 打印的单位是ns, 10的9次方ns=1s
        System.out.println(sw.prettyPrint());
        // 打印总的耗时
        System.out.println(sw.getTotalTimeSeconds());
    }
}
